#include "mainwindow.h"
#include "databasesutils.h"
#include "logindialog.h"
#include "studentdatabaseconst.h"
#include "ui_mainwindow.h"
#include <QDialog>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlRelationalDelegate>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);

  QMenu *fileMenu = new QMenu("File", this);
  fileMenu->addAction(tr("登录"), [this]() {
    // TODO 为何加入parent就会重复释放?
    LoginDialog *login = new LoginDialog(nullptr);
    if (QDialog::Accepted == login->exec()) {
      QMessageBox::information(this, tr("成功"), tr("登陆成功"));
      delete login;
      logined = true;
    }

  });
  ui->menuBar->addMenu(fileMenu);

  auto database = DataBasesUtils::getInstance();
  database.open();
  sqlmodel = new QSqlRelationalTableModel(this, database);
  //  sqlmodel->setTable("student");
  sqlmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
  //  sqlmodel->select();
  //  sqlmodel->setHeaderData(1,Qt::Horizontal,tr("姓名"));
  //  sqlmodel->setHeaderData(2,Qt::Horizontal,tr("年龄"));

  QVBoxLayout *ctwdg = new QVBoxLayout(this);

  QVBoxLayout *vlayout = new QVBoxLayout(this);

  QHBoxLayout *tplayout = new QHBoxLayout(this);

  tv = new QTableView(this);
  tv->setModel(sqlmodel);
  tv->setItemDelegate(new QSqlRelationalDelegate(this));
  tv->hideColumn(0);
  tv->show();
  vlayout->addItem(tplayout);
  vlayout->addWidget(tv);

  QPushButton *studentTable = new QPushButton(tr("学生"), this);
  QPushButton *courseTable = new QPushButton(tr("课程"), this);
  QPushButton *scTable = new QPushButton(tr("成绩"), this);
  tplayout->addWidget(studentTable);
  tplayout->addWidget(courseTable);
  tplayout->addWidget(scTable);

  QHBoxLayout *bbutton = new QHBoxLayout(this);
  QPushButton *commit = new QPushButton(tr("提交"), this);
  QPushButton *revert = new QPushButton(tr("撤销"), this);
  bbutton->addWidget(commit);
  bbutton->addWidget(revert);

  QHBoxLayout *tablemidright = new QHBoxLayout(this);
  tablemidright->addItem(vlayout);

  QVBoxLayout *rightButton = new QVBoxLayout(this);
  QPushButton *insert = new QPushButton(tr("新增"), this);
  QPushButton *deleterow = new QPushButton(tr("删除"), this);
  rightButton->addWidget(insert);
  rightButton->addWidget(deleterow);

  tablemidright->addItem(rightButton);
  ctwdg->addItem(tablemidright);
  ctwdg->addItem(bbutton);
  ui->centralWidget->setLayout(ctwdg);

  connect(revert, &QPushButton::clicked, [=]() { sqlmodel->revertAll(); });
  connect(studentTable, &QPushButton::clicked, [=]() {
    if (logined) {
      sqlmodel->setTable("student");
      sqlmodel->setSort(0, Qt::SortOrder::AscendingOrder);
      sqlmodel->select();
      tv->resizeColumnsToContents();
    } else {
      QMessageBox::warning(this, tr("错误"), tr("请登录"));
    }

  });
  connect(courseTable, &QPushButton::clicked, [=]() {
    if (logined) {
      sqlmodel->setTable("course");
      sqlmodel->setSort(0, Qt::SortOrder::AscendingOrder);
      sqlmodel->select();
      tv->resizeColumnsToContents();
    } else {
      QMessageBox::warning(this, tr("错误"), tr("请登录"));
    }

  });
  connect(scTable, &QPushButton::clicked, [=]() {
    if (logined) {
      sqlmodel->setTable("sc");
      sqlmodel->setSort(0, Qt::SortOrder::AscendingOrder);
      sqlmodel->setRelation(scsno, QSqlRelation("student", "sno", "sno"));
      sqlmodel->setRelation(sccno, QSqlRelation("course", "cno", "cno"));
      sqlmodel->select();
      tv->resizeColumnsToContents();
    } else {
      QMessageBox::warning(this, tr("错误"), tr("请登录"));
    }

  });
  connect(commit, &QPushButton::clicked, [=]() {
    qDebug() << sqlmodel->submitAll();
    qDebug() << sqlmodel->lastError();
  });
  connect(deleterow, &QPushButton::clicked, [=]() {
    auto ret = sqlmodel->removeRow(tv->currentIndex().row());
    auto error = sqlmodel->lastError().text();
    if (!ret) {
      QMessageBox::warning(this, tr("失败"), tr("操作失败：") + error);
      sqlmodel->revertAll();
    }

  });
  connect(insert, &QPushButton::clicked, [=]() {
      sqlmodel->insertRow(sqlmodel->rowCount());


  });
}

bool MainWindow::logined = false;
MainWindow::~MainWindow() { delete ui; }
